package cn.gene.timeyan.security.sso.controller;

import cn.gene.timeyan.common.restful.JsonResult;
import cn.gene.timeyan.pojo.valid.security.dto.UpdatePwdDTO;
import cn.gene.timeyan.pojo.valid.security.dto.UserCodeLoginDTO;
import cn.gene.timeyan.pojo.valid.security.dto.validateCodeDTO;
import cn.gene.timeyan.security.sso.service.IUserValidationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 验证码*
 *
 * @author LWG
 * @date 2022/8/26 21:27
 * @since 1.0.0 @gene.cn
 */
@RestController
@RequestMapping("/userValidation")
@Api(tags = "用户验证码发送以及验证相关接口")
public class UserValidationController {
    @Autowired
    IUserValidationService userValidationService;

    @ApiOperation(value = "发送验证短信")
    @GetMapping("/sendCodeByPhone")
    public JsonResult<String> sendCodeByPhone(@RequestParam("phone") String phone){
        System.out.println(phone);
        return userValidationService.sendCodeByPhone(phone);
    }
    @ApiOperation(value = "发送验证邮箱")
    @GetMapping("/sendCodeByEmail")
    public JsonResult<String> sendCodeByEmail(@RequestParam("email") String email){
        System.out.println(email);
        return userValidationService.sendCodeByEmail(email);
    }

    @ApiOperation(value = "校验验证码")
    @PostMapping("/checkCode")
    public JsonResult<String> checkCode(@RequestBody UserCodeLoginDTO userCodeLoginDTO){
        System.out.println(userCodeLoginDTO);
        return userValidationService.checkCode(userCodeLoginDTO);
    }

    @ApiOperation(value = "修改密码")
    @PostMapping("/updatepwd")
    public JsonResult<String> updatepwd(@RequestBody UpdatePwdDTO updatePwdDTO, HttpServletRequest request){
        System.out.println(updatePwdDTO);
        return userValidationService.updatepwd(updatePwdDTO,request);
    }
    /**
     * 发送验证邮件
     *
     * @return
     */
//    @ApiOperation(value = "发送验证邮件", notes = "调用sendValidEmail方法，使用userID参数来发送验证邮件")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "userID", value = "用户ID", paramType = "int", required = true)
//    })
//    @GetMapping("/sendValidEmail")
//    Result sendValidEmail(@RequestParam("userID") int userID) {
//        userValidationService.sendEmail(userID);
//        return new Result(null, States.OK.getStates(), States.OK.getDescription());
//    }

    /**
     * 发送验证短信
     *
     * @return
     */
    @ApiOperation(value = "发送验证短信", notes = "调用sendValidSms方法，使用userID参数来发送验证邮件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userID", value = "用户ID", paramType = "int", required = true)
    })
    @GetMapping("/sendValidSms")
    public JsonResult sendValidSms(@RequestParam("userID") int userID) {
        userValidationService.sendSms(userID);
        return JsonResult.ok("发送成功");
    }

    /**
     * 发送登录验证邮件
     *
     * @return
     */
//    @ApiOperation(value = "发送登录验证邮件", notes = "若用户库中无此邮件则抛出异常")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "email", value = "要发送的邮箱", paramType = "String", required = true)
//    })
//    @GetMapping("/sendValidEmailForLogin")
//    Result sendValidEmailForLogin(@RequestParam("email") String email) {
//        userValidationService.sendEmailForLogin(email);
//        return new Result(null, States.OK.getStates(), States.OK.getDescription());
//    }

    /**
     * 发送登录验证短信
     *
     * @return
     */
    @ApiOperation(value = "发送登录验证短信", notes = "若手机号未注册则发送失败")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "发送的手机号", paramType = "String", required = true)
    })
    @GetMapping("/sendValidSmsForLogin")
    public JsonResult sendValidSmsForLogin(@RequestParam("phone") String phone) {
        userValidationService.sendSmsForLogin(phone);
        return JsonResult.ok("发送验证码成功");
    }

    /**
     * 验证验证码
     *
     * @return
     */
    @ApiOperation(value = "验证验证码", notes = "调用validateCode方法来发送验证验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "loginKey", value = "手机号或邮箱", required = true),
            @ApiImplicitParam(name = "code", value = "验证码", required = true),
            @ApiImplicitParam(name = "validateCodeDTO", value = "以上两个参数封装在此DTO中")
    })
    @PostMapping("/validateCode")
    public JsonResult validateCode(@Validated @RequestBody validateCodeDTO validateCodeDTO) {
        userValidationService.validateCode(validateCodeDTO);
        return JsonResult.ok("验证通过");
    }
}


